﻿using UnityEngine;

public class Bars : MonoBehaviour
{
    [Header("排序层")]
    public string sortingLayerName = "Default";

    [Header("层索引")]
    public int orderInlayer = 0;

    private Mesh mh;
    private Renderer rd;

    private float rate = 0.5f;
    public Material mat;

    private int barIndex = 0;
    private bool init = false;

    private float bigWidth = 124;
    private float smallWidth = 112;
    float height = 20f;

    public enum BarType
    {
        HP,
        MP,
        SP,
        EXP
    }

    [Header("条类型")]
    [SerializeField]
    public BarType _barType;

    [Header("当前容量-0到1")]
    [SerializeField]
    private float _percent;

    public float Percent
    {
        get
        {
            return _percent;
        }

        set
        {
            _percent = value;
            if (null != mh)
            {
                this.SetBarRate(1 - this._percent);
            }
        }
    }

    void Awake()
    {
        mh = GetComponent<MeshFilter>().mesh;
        rd = GetComponent<MeshRenderer>();

        rd.sortingOrder = this.orderInlayer;

        rd.sortingLayerName = this.sortingLayerName;

    }

    void Start()
    {
        if (init)
            return;
        init = true;
        switch (this._barType)
        {
            case BarType.HP:
                this.barIndex = 2;
                break;
            case BarType.MP:
                this.barIndex = 1;
                break;
            case BarType.SP:
                this.barIndex = 3;
                break;
            case BarType.EXP:
                this.barIndex = 1;
                break;
            default:
                break;
        }
        CreateBar(new Vector2(0.6f, 0.1f), this.barIndex);
        SetBarRate(1 - this.Percent);

    }

    /// <summary>
    /// 利用网格创建血条
    /// </summary>
    /// <param name="size">三角形大小</param>
    /// <param name="barIndex">血条索引</param>
    void CreateBar(Vector2 size, int barIndex)
    {
        this.barIndex = barIndex;

        float widthOutter = size.x;
        float widthInner = size.x * smallWidth / bigWidth;

        // widthInner = 0.510f;

        //顶点数组
        Vector3[] vertes = new Vector3[]
        {
            new Vector3(-widthOutter, -size.y, 0),//第一个点
            new Vector3(-widthOutter, size.y, 0), //第二个
            new Vector3(widthOutter, size.y, 0), //第三个
            new Vector3(widthOutter, -size.y, 0), //第四个

            new Vector3(-widthInner, -size.y, 0),//第一个点
            new Vector3(-widthInner, size.y, 0), //第二个
            new Vector3(widthInner, size.y, 0), //第三个
            new Vector3(widthInner, -size.y, 0), //第四个
        };

        //给网格的顶点赋值
        mh.vertices = vertes;

        //顶点组成的三角形
        mh.triangles = new[]
        {
            0, 1, 2,
            0, 2, 3,

            4,5,6,
            4,6,7
        };

        //UV贴图的四个点，和顶点一一对应，左下角为(0,0)，右上角为(1,1)
        //如果顶点顺序没有跟UV对应，贴图就会出现问题
        Vector2[] uvs = new Vector2[]
        {
            new Vector2(0, 0.25f * 0),//第一个点
            new Vector2(0, 0.25f * (0+1)),//2
            new Vector2(1f , 0.25f * (0+1)),//3
            new Vector2(1f , 0.25f * 0), //4

            new Vector2(0.25f, 0.25f * barIndex),//第一个点
            new Vector2(0.25f, 0.25f * (barIndex+1)),//2
            new Vector2(0.75f , 0.25f * (barIndex+1)),//3
            new Vector2(0.75f , 0.25f * barIndex), //4
        };

        mh.uv = uvs;

        //材质
        rd.material = mat;

        //法线重新计算
        mh.RecalculateNormals();

    }

    /// <summary>
    /// 设置血条比例
    /// </summary>
    /// <param name="value">血量失去的百分比</param>
    void SetBarRate(float value)
    {
        if(mh.uv.Length == 0)
        {
            Start();
        }
        value *= 0.5f;
        Vector2[] uvs = new Vector2[]
        {
            new Vector2(0, 0.25f * 0),//第一个点
            new Vector2(0, 0.25f * (0+1)),//2
            new Vector2(1f , 0.25f * (0+1)),//3
            new Vector2(1f , 0.25f * 0), //4

            new Vector2(value, 0.25f * barIndex),//第一个点
            new Vector2(value, 0.25f * (barIndex+1)),//2
            new Vector2(0.5f + value , 0.25f * (barIndex+1)),//3
            new Vector2(0.5f + value, 0.25f * barIndex), //4
        };
        if(mh.uv.Length != uvs.Length)
        {
            Debug.Log("1111");
        }
        mh.uv = uvs;
    }


}
